package cn.jinjimi.app.controller;


import cn.jinjimi.app.AccountManager;
import cn.jinjimi.domain.data.net.ApiConstants;
import cn.jinjimi.app.controller.event.UploadResultEvent;
import cn.jinjimi.domain.data.dto.NoticeDto;
import cn.jinjimi.domain.ServiceManager;
import cn.jinjimi.domain.store.ConfigStore;
import cn.jinjimi.domain.store.FileStore;
import cn.jinjimi.domain.store.ReaderStore;
import cn.jinjimi.domain.store.TaskStore;
import cn.jinjimi.app.entity.Article;
import cn.jinjimi.app.entity.Products;
import cn.jinjimi.app.entity.SharedCount;
import cn.jinjimi.app.entity.TaskListBean;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;

import bolts.Task;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Rain on 2016/1/15.
 */
public class CommonController extends BaseController {

    private static final String TAG = CommonController.class.getSimpleName();

    /**
     * 获取分享了的数目的接口
     *
     * @return
     */
    public static Task<SharedCount> getShareCountCount() {

        return Task.callInBackground(new Callable<SharedCount>() {
            @Override
            public SharedCount call() throws Exception {
                return ServiceManager.getService(ReaderStore.class).fetchSharedCount();
            }
        });
    }

    /**
     * 分享到向日葵
     * Event ShareXrkEvent
     * E Article
     *
     * @param articleId
     * @return
     */
    public static Task<Article> share2Xrk(final String articleId, final String comment, final String showAds) {
        return Task.callInBackground(new Callable<Article>() {
            @Override
            public Article call() throws Exception {
                return ServiceManager.getService(ReaderStore.class).recordShare(articleId, comment, showAds);
            }
        });
    }

    /**
     * Event TaskListEvent
     * 获取任务列表
     *
     * @return
     */
    public static Task<List<TaskListBean.TaskBean>> getTaskList() {

        return Task.callInBackground(new Callable<List<TaskListBean.TaskBean>>() {
            @Override
            public List<TaskListBean.TaskBean> call() throws Exception {
                return ServiceManager.getService(TaskStore.class).getTaskList();
            }
        });

    }


    /**
     * 提交完成一个任务操作
     *
     * @param action_type
     * @return
     */
    public static Task<Boolean> uploadTaskRecord(final int action_type) {
        return Task.callInBackground(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return ServiceManager.getService(TaskStore.class).recordTask(
                        AccountManager.getInstance().getAccount().ssoid, action_type
                );
            }
        });
    }

    /**
     * 获取发现中的产品列表
     *
     * @return
     */
    public static Task<List<Products>> getProducts() {

        return Task.callInBackground(new Callable<List<Products>>() {
            @Override
            public List<Products> call() throws Exception {
                return ServiceManager.getService(ConfigStore.class).getProducts();
            }
        });
    }

    public static Task<Boolean> postShareRecord(final String ssoid, final String url, final String platform) {
        return Task.callInBackground(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return ServiceManager.getService(TaskStore.class).recordShare(ssoid, url, platform);
            }
        });
    }


    /**
     * 上传文件请求
     *
     * @param url
     * @param params
     * @return
     */
    public static Task<UploadResultEvent> uploadFile(final String url, final HashMap<String, Object> params) {
        return Task.callInBackground(new Callable<UploadResultEvent>() {
            @Override
            public UploadResultEvent call() throws Exception {
                return ServiceManager.getService(FileStore.class).upload(url, params);
            }
        });
    }

    /**
     * 同步的上传客户头像方法
     *
     * @param customerId
     * @param avatarPath
     * @return
     * @throws IOException
     */
    public static UploadResultEvent uploadCustomFile(long customerId, String avatarPath) throws IOException {
        return new UploadResultEvent();
    }


    /**
     * 获取“红点”提示列表
     *
     * @return
     */
    public static Task<NoticeDto> getRedRemindList() {
        return Task.callInBackground(new Callable<NoticeDto>() {
            @Override
            public NoticeDto call() throws Exception {
                return ServiceManager.getService(ConfigStore.class).getNoticeList();
            }
        });

    }

}
